package com.qintian.rainstorm.test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 
 * 气象局暴雨监测预报返回结果pojo
 *
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class WeatherInfoBo {
	private static final ObjectMapper MAPPER = new ObjectMapper();
	private Integer returnCode;
	private Integer rowCount;
	private Integer colCount;
	private String returnMessage;
	private Date time;
	private String stationcode;
	private String count;
	
	private Object DS;
	// 获取预报时间的小时数值
	private String predhour;
	// 获取当前时间的小时数值
	private String nowhour;
	
	// 起始维度
	private String startLat;
	
	// 结束维度
	private String endLat;
	
	// 起始经度
	private String startLon;
	
	// 结束经度
	private String endLon;
	
	private Integer latCount;
	
	private Integer lonCount;
	
	private String latStep;
	
	private String lonStep;

	public String getStartLat() {
		return startLat;
	}

	public void setStartLat(String startLat) {
		this.startLat = startLat;
	}

	public String getEndLat() {
		return endLat;
	}

	public void setEndLat(String endLat) {
		this.endLat = endLat;
	}

	public String getStartLon() {
		return startLon;
	}

	public void setStartLon(String startLon) {
		this.startLon = startLon;
	}

	public String getEndLon() {
		return endLon;
	}

	public void setEndLon(String endLon) {
		this.endLon = endLon;
	}

	public Integer getLatCount() {
		return latCount;
	}

	public void setLatCount(Integer latCount) {
		this.latCount = latCount;
	}

	public Integer getLonCount() {
		return lonCount;
	}

	public void setLonCount(Integer lonCount) {
		this.lonCount = lonCount;
	}

	public String getLatStep() {
		return latStep;
	}

	public void setLatStep(String latStep) {
		this.latStep = latStep;
	}

	public String getLonStep() {
		return lonStep;
	}

	public void setLonStep(String lonStep) {
		this.lonStep = lonStep;
	}

	public static ObjectMapper getMapper() {
		return MAPPER;
	}

	public String getPredhour() {
		return predhour;
	}

	public void setPredhour(String predhour) {
		this.predhour = predhour;
	}

	public String getNowhour() {
		return nowhour;
	}

	public void setNowhour(String nowhour) {
		this.nowhour = nowhour;
	}

	public Integer getReturnCode() {
		return returnCode;
	}

	public void setReturnCode(Integer returnCode) {
		this.returnCode = returnCode;
	}

	public Integer getRowCount() {
		return rowCount;
	}

	public void setRowCount(Integer rowCount) {
		this.rowCount = rowCount;
	}

	public Integer getColCount() {
		return colCount;
	}

	public void setColCount(Integer colCount) {
		this.colCount = colCount;
	}

	public String getReturnMessage() {
		return returnMessage;
	}

	public void setReturnMessage(String returnMessage) {
		this.returnMessage = returnMessage;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public Object getDS() {
		return DS;
	}

	public void setDS(Object dS) {
		DS = dS;
	}

	public String getStationcode() {
		return stationcode;
	}

	public void setStationcode(String stationcode) {
		this.stationcode = stationcode;
	}

	public String getCount() {
		return count;
	}

	public void setCount(String count) {
		this.count = count;
	}

	public WeatherInfoBo() {

	}

	public WeatherInfoBo(Integer returnCode, String returnMessage, Integer rowCount, Integer colCount, Date time,
			Object DS) {
		this.returnCode = returnCode;
		this.returnMessage = returnMessage;
		this.rowCount = rowCount;
		this.colCount = colCount;
		this.time = time;
		this.DS = DS;
	}

	/**
	 * 将json结果转化为WeatherInfo对象
	 */
	public static WeatherInfoBo formatToPojo(String jsonData, Class<?> clazz) {
		try {
			if (clazz == null) {
				return MAPPER.readValue(jsonData, WeatherInfoBo.class);
			}
			JsonNode jsonNode = MAPPER.readTree(jsonData);
			JsonNode data = jsonNode.get("DS");
			Object obj = null;
			if (clazz != null) {
				if (data.isObject()) {
					obj = MAPPER.readValue(data.traverse(), clazz);
				} else if (data.isTextual()) {
					obj = MAPPER.readValue(data.asText(), clazz);
				}
			}

			boolean flag = jsonNode.has("time");
			WeatherInfoBo info;
			if (!flag) {
				info = new WeatherInfoBo(jsonNode.get("returnCode").intValue(), jsonNode.get("returnMessage").asText(),
						null, null, null, obj);
			} else {
				Date time = DateFormatUtil.formatToDate(jsonNode.get("time").asText());
				info = new WeatherInfoBo(jsonNode.get("returnCode").intValue(), jsonNode.get("returnMessage").asText(),
						null, null, time, obj);
			}
			return info;
		} catch (Exception e) {
			return null;
		}

	}

	public static WeatherInfoBo format(String json) {
		try {
			return MAPPER.readValue(json, WeatherInfoBo.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * object 是集合转化
	 * 
	 * @param jsonData
	 *            json数据
	 * @param clazz
	 *            集合中的类型
	 * @return
	 */
	public static WeatherInfoBo formatToList(String jsonData, Class<?> clazz) {
		try {
			JsonNode jsonNode = MAPPER.readTree(jsonData);
			JsonNode data = jsonNode.get("DS");
			Object obj = null;
			if (data.isArray() && data.size() > 0) {
				obj = MAPPER.readValue(data.traverse(),
						MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
			}
			boolean flag = jsonNode.has("time");
			WeatherInfoBo info;
			if (!flag) {
				info = new WeatherInfoBo(jsonNode.get("returnCode").intValue(), jsonNode.get("returnMessage").asText(),
						null, null, null, obj);
			} else {
				Date date = DateFormatUtil.formatToDate(jsonNode.get("time").asText());
				info = new WeatherInfoBo(jsonNode.get("returnCode").intValue(), jsonNode.get("returnMessage").asText(),
						null, null, date, obj);
			}
			return info;
		} catch (IOException e) {
			return null;
		}
	}
	
	/**
	 * object 是集合转化
	 * 
	 * @param jsonData
	 *            json数据
	 * @param clazz
	 *            集合中的类型
	 * @return
	 */
	public static WeatherInfoBo formatToData(String jsonData, Class<?> clazz) {
		try {
			JsonNode jsonNode = MAPPER.readTree(jsonData);
			JsonNode data = jsonNode.get("DS");		   
			Object obj = null;
			if (data.isArray() && data.size() > 0) {
				JsonNode jsondata = MAPPER.readTree(data.toString());
				String dataNode = jsondata.get(0).toString();
			    String objstr =dataNode.substring(9,dataNode.length()-1); 
			    obj = MAPPER.readValue(objstr,List.class);			    
			}
			boolean flag = jsonNode.has("time");
			WeatherInfoBo info;
			if (!flag) {
				info = new WeatherInfoBo(jsonNode.get("returnCode").intValue(), jsonNode.get("returnMessage").asText(),
						null, null, null, obj);
			} else {
				Date date = DateFormatUtil.formatToDate(jsonNode.get("time").asText());
				info = new WeatherInfoBo(jsonNode.get("returnCode").intValue(), jsonNode.get("returnMessage").asText(),
						null, null, date, obj);
			}
			return info;
		} catch (IOException e) {
			return null;
		}
	}

}
